#if defined(__linux__)
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include <signal.h>
#include <iostream>
		



#include <gflags/gflags.h>
#include <butil/logging.h>
#include <butil/time.h>
#include <brpc/channel.h>
#include "frpc.pb.h"
#include <frpc/auth.h>
#include <filestore/file_store.h>
#include <filestore/bar_file.h>
//#include <filestore/tdx_store.h>
#include <cppbase/utility.h>
#include <handler/run.h>
DEFINE_string(attachment, "", "Carry this along with requests");
DEFINE_string(protocol, "baidu_std", "Protocol type. Defined in src/brpc/options.proto");
DEFINE_string(connection_type, "", "Connection type. Available values: single, pooled, short");
DEFINE_string(server, "192.168.10.5:8000", "IP Address of server");
DEFINE_string(server_, "127.0.0.1:8000", "IP Address of server");
DEFINE_string(load_balancer, "", "The algorithm for load balancing");
DEFINE_int32(timeout_ms, -1, "RPC timeout in milliseconds");
DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)"); 
DEFINE_int32(interval_ms, 1000, "Milliseconds between consecutive requests");

int main2(int argc, char* argv[]) {
     AppGlobal::instance()->init("../sa_config.txt");
     BarFile::set_rootpath(AppGlobal::instance()->get_config("bar_path"));

    brpc::Channel channel;
    
    // Initialize the channel, NULL means using default options.
    brpc::ChannelOptions options;
    options.protocol = FLAGS_protocol;
    options.connection_type = FLAGS_connection_type;
    options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
    options.max_retry = FLAGS_max_retry;
    frpc::Auth auth;
    options.auth=&auth;
    if (channel.Init(FLAGS_server_.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {
        LOG(ERROR) << "Fail to initialize channel";
        return -1;
    }


    frpcType::StaticDataSvc_Stub stub(&channel);

    // Send a request and wait for the response every 1 second.
    
    frpcType::GetBarRequest request;
    frpcType::DefaultArg response;
    brpc::Controller cntl;
    request.set_barsize(int(BarSize::Day1));//1d
    request.set_rbegin(0);
    request.set_count(100);
    request.set_exchange_id(1);//shanghai 
    request.set_symbol("000002");

    stub.GetBarSeries(&cntl, &request, &response, NULL);
        if (!cntl.Failed()) {
            LOG(INFO) << "Received response from " << cntl.remote_side()
                << " to " << cntl.local_side()
                << ",size: " << response.ByteSizeLong() << " (attached size="
                << cntl.response_attachment().size() << ")"
                << " latency=" << cntl.latency_us() << "us";
        } else {
            LOG(WARNING) << cntl.ErrorCode()<<",txt:"<<cntl.ErrorText();
        }

     
    LOG(INFO) << "end, to quit";
    return 0;
}


int main(int argc, char* argv[]) {
    AppGlobal::instance()->init("../sa_config.txt");
    BarFile::set_rootpath("/home/y/tmpbar");

    brpc::Channel channel;
    brpc::ChannelOptions options;
    options.protocol = FLAGS_protocol;
    options.connection_type = FLAGS_connection_type;
    options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
    options.max_retry = FLAGS_max_retry;
    frpc::Auth auth;
    options.auth = &auth;
    if (channel.Init(FLAGS_server_.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {
      //  LOG(ERROR) << "Fail to initialize channel";
        return -1;
    }


    frpcType::AdminSvc_Stub stub(&channel);


    frpcType::UpdateBarRequest request;
    frpcType::DefaultArg response;
    brpc::Controller cntl;
    BarFile barf(Exchange(1),"000001",BarSize::Day1);
    request.set_barsize(int(BarSize::Day1));//1d
    request.set_exchange_id(1);//shanghai 
    request.set_symbol("000001");
    int index=0;
    int count=100;

    while (true) {
        //cntl.request_attachment().clear();
        cntl.Reset();
        int cnt=count;
        auto ptr=barf.get_bar(index,&cnt);
        for(int i=0;i<100;i++){
            LOG(INFO)<<*(uint32_t*)(ptr.get()+32*i);
        }
        request.set_rowsize(32);
        request.set_rowcount(cnt);
        cntl.request_attachment().clear();
        cntl.request_attachment().append((char*)ptr.get() , cnt * sizeof(Bar));

        stub.UpdateBarSeries(&cntl, &request, &response, NULL);
        if (!cntl.Failed()) {
            LOG(INFO) << "Received response from " << cntl.remote_side()
                << " to " << cntl.local_side()
                << ",size: " << response.ByteSizeLong() << " (attached size="
                << cntl.response_attachment().size() << ")"
                << " latency=" << cntl.latency_us() << "us";
        }
        else {
            LOG(WARNING) << cntl.ErrorCode() << ",txt:" << cntl.ErrorText();
            break;
        }
  
        
        index += cnt;

        if (brpc::IsAskedToQuit() || cnt<100)
            break;
    }

    LOG(INFO) << "end, to quit";
    return 0;
}